﻿Imports System.IO

Public Class MetaXtraReport
    Inherits DevExpress.XtraReports.UI.XtraReport

    Public Property QueryName As String
    Public Property ReportName As String

    Private Property mq As MetaQuery

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal reportName As String)
        MyBase.New()
        InitDataReport(reportName)
    End Sub

    Public Sub InitDataReport(ByVal reportName As String)
        InitReport(reportName)
        InitData()
    End Sub

    Public Sub InitDataReport()
        InitReport(ReportName)
        InitData()
    End Sub

    Public Sub InitData()
        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
        Dim aParamFrm As MqParamForm
        Dim aExecQuery As DialogResult = DialogResult.OK

        Try
            mq = MetaQuery.GetMetaQueryByName(QueryName)
            If mq IsNot Nothing Then
                If mq.MetaParams.Rows.Count > 0 Then
                    aParamFrm = New MqParamForm(mq.QueryName)
                    aExecQuery = aParamFrm.ShowDialog()
                    'On dit OK pour la form de Parametre
                    If aExecQuery = DialogResult.OK Then
                        'On va chercher les Parametre de la SP
                        'sqlcmd = MetaQuery.GetSPCmdByMeta(aMq, False)
                        sqlcmd = mq.InitQueryParams

                        'On prend les parametre défini par la MQ, pour les retrouver dans le sqlCmd.Parameters
                        'Les metaColumns on la priorité
                        Dim param As SqlParameter
                        For Each row As DataRow In mq.MetaParams.Rows
                            If sqlcmd.Parameters.Contains(row("ColumnName").ToString) Then
                                param = sqlcmd.Parameters(row("ColumnName").ToString)
                                If param IsNot Nothing AndAlso Not IsDBNull(aParamFrm.SelectedParam(row("ColumnName").ToString)) Then
                                    param.Value = aParamFrm.SelectedParam(row("ColumnName").ToString)
                                End If
                            Else
                                MessageWindow.ShowMessage(New Exception(String.Format("MetaExtraReport Execution, SQL Parameter not found: {0}", row("ColumnName"))))
                            End If
                        Next
                        'On Passe la sqcmd qui a les paramêtre de rentré
                        mq.FillResultQuery(sqlCmd:=sqlcmd)
                        MyBase.DataSource = mq.ResultQuery
                    End If
                End If
            Else
                Dim ex As Exception = New Exception(String.Format("QueryName: {0} Not Found Found For Report {1}", QueryName, ReportName))
                MessageWindow.ShowMessage(ex)
            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex)
        End Try
    End Sub

    Public Sub InitReport(ByVal reportName As String)
        Dim data() As Byte
        Dim encoder As System.Text.UTF8Encoding
        Dim reportString As String

        Dim dataset As DataSet
        Try
            Using mqReport As MetaQuery = MetaQuery.GetMetaQueryByName("ReadReportFromDocTemplate")
                dataset = New DataSet()
                mqReport.SQLWhere = String.Format("Name = '{0}' ", reportName)
                mqReport.FillResultQuery()
                '
                If mqReport.ResultQuery.Rows.Count = 1 Then
                    'BUGBUG: SNK of SCIEF in String...
                    reportString = mqReport.ResultQuery.Rows(0).Item("Data").ToString
                    Using msXmlReportLayout As MemoryStream = New MemoryStream()
                        encoder = New System.Text.UTF8Encoding()
                        '
                        data = encoder.GetBytes(reportString)
                        msXmlReportLayout.Write(data, 0, data.Length)
                        '
                        Me.LoadLayoutFromXml(msXmlReportLayout)
                        '
                        'On a pas assigné la QueryName donc on prend celle definit dans le rapport sauvgardé
                        If String.IsNullOrWhiteSpace(QueryName) Then
                            dataset.ReadXmlSchema(Me.DataSourceSchema)
                            QueryName = dataset.DataSetName
                        End If
                    End Using
                End If
            End Using
        Catch ex As Exception
            MessageWindow.ShowMessage(ex)
        End Try

    End Sub

End Class
